using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Beta_fundies_controls_comments_CommentPage : Controls.DataManagerQuoteControl
{
    private int CurrentPage
    {
        get
        {
            int page;
            if (Helpers.QueryStringHelper.TryGetInt32("page", out page))
            {
                return page;
            }
            else
            {
                return 1;
            }
        }
    }

    protected override void ItemChanged()
    {
        Refresh();
    }

    private void Refresh()
    {
        if (CurrentQuote != null)
        {
            mviewComments.SetActiveView(viewComments);
            Page.Title = Helpers.StringHelper.SafeFormat("{0}, {1}", CurrentQuote.Fundie, CurrentQuote.Board);

            List<DAL.Quote> quoteDataSource = new List<DAL.Quote>(1);
            quoteDataSource.Add(CurrentQuote);
            QuoteListl1.SetDataSource(quoteDataSource);

            SubmitComment1.CurrentQuote = CurrentQuote;


            DAL.DataManagerPage<DAL.Comment> commentDataSource = DAL.Comments.GetByQuoteAndPage(CurrentQuote, CurrentPage);
            Comments1.SetDataSource(commentDataSource.Items);
            Pager1.TotalPages = commentDataSource.Pages;
            Pager1.UrlFormat = "~/fundies/comments.aspx?q=" + CurrentQuote.ID + "&page={0}";
            Pager1.CurrentPage = CurrentPage;
            Pager2.TotalPages = Pager1.TotalPages;
            Pager2.UrlFormat = Pager1.UrlFormat;
            Pager2.CurrentPage = Pager1.CurrentPage;

            HandleModPanel();
        }
        else
        {
            mviewComments.SetActiveView(viewError);
            Page.Title = "Quote Not Found";
        }
    }

    private void HandleModPanel()
    {
        DAL.User user = Managers.UserLoginManager.CurrentUser;
        if (user != null && user.HasPermissions(DAL.User.UserStatus.Moderator))
        {
            pnlMod.Visible = true;
            if (CurrentQuote.CommentState == DAL.Quote.CommentStatus.Open)
            {
                btnClose.Text = "Close This Thread";
            }
            else
            {
                btnClose.Text = "Open This Thread";
            }
        }
        else
        {
            pnlMod.Visible = false;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!Page.IsPostBack)
        //{
            CurrentQuote = GetItemFromQueryString();
        //}
    }

    protected void btnClose_Click(object sender, EventArgs e)
    {
        if (CurrentQuote != null)
        {
            if (CurrentQuote.CommentState == DAL.Quote.CommentStatus.Open)
            {
                CurrentQuote.CommentState = DAL.Quote.CommentStatus.Closed;
            }
            else
            {
                CurrentQuote.CommentState = DAL.Quote.CommentStatus.Open;
            }
            
            DAL.Quotes.Save(CurrentQuote);
            Refresh();
        }
    }

    protected void CommentCreated(object sender, EventArgs e)
    {
        if (CurrentQuote != null)
        {
            DAL.DataManagerPage<DAL.Comment> latestComment = DAL.Comments.GetLatestCommentByQuote(CurrentQuote);
            if (latestComment.Items.Count > 0)
            {
                HttpContext.Current.Response.Redirect(String.Format("~/fundies/comments.aspx?q={0}&page={1}#{2}",
                    CurrentQuote.ID,
                    latestComment.Pages,
                    latestComment.Items[0].ID));
            }
        }
    }
}
